﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Windows.Controls;
//using System.Web.UI.WebControls;


namespace proyecto
{
    /// <summary>
    /// Lógica de interacción para loadDataWindow.xaml
    /// </summary>
    public partial class viewDataTable : UserControl
    {

        private DataTable _datagrid;
        private DataTable copy;

        ComboBox[] types;
        Button[] changeNames;
        CheckBox[] load;

        int columnMouseOver;

        ContextMenu context;

        MenuItem item;

        bool canceled;


        public viewDataTable()
        {
            InitializeComponent();
        }

        public viewDataTable(DataTable matrix)
        {
            canceled = true;

            _datagrid = new DataTable();
            copy = new DataTable();
            //_datagrid = matrix.getTable();

            table = new DataGrid();

            dockPanel1 = new DockPanel();

            StackPanel stackPanel1 = new StackPanel();
            StackPanel stackpanel2 = new StackPanel();
            StackPanel buttonsPanel = new StackPanel();
            StackPanel checkPanel = new StackPanel();
            DockPanel.SetDock(stackPanel1, Dock.Top);



            stackPanel1.HorizontalAlignment = HorizontalAlignment.Stretch;
            stackPanel1.Height = 25;

            for (int i = 0; i < matrix.Columns.Count; i++)
            {
                DataColumn column = new DataColumn(matrix.Columns[i].Caption);
                //column = matrix.getColumn(i);
                _datagrid.Columns.Add(column);
                DataColumn column2 = new DataColumn(matrix.Columns[i].Caption);
                copy.Columns.Add(column2);


            }

            context = new ContextMenu();

            item = new MenuItem();

            item.Header = "Rename column";


            item.Click += new RoutedEventHandler(Change_name);

            context.Items.Add(item);

            //table.MouseRightButtonUp+= new MouseButtonEventHandler(table_MouseUp);

            //table.ContextMenu = context;

            /*Setter set = new Setter();
            set.Property = DependencyProperty.Register("ContextMenu", typeof(object), null, null);// ("ContextMenu" as object) as DependencyProperty;
            set.Value = ("context" as object);
            table.Columns[1].HeaderStyle.Setters.Add(set);*/


            types = new ComboBox[matrix.Columns.Count];
            changeNames = new Button[matrix.Columns.Count];
            load = new CheckBox[matrix.Columns.Count];
            //MessageBox.Show("aqui");
            DataRow row = _datagrid.NewRow();
            stackPanel1.Orientation = Orientation.Horizontal;
            buttonsPanel.Orientation = Orientation.Horizontal;
            checkPanel.Orientation = Orientation.Horizontal;
            stackPanel1.Margin = new Thickness(5);
            buttonsPanel.Margin = new Thickness(5);
            checkPanel.Margin = new Thickness(5);

            for (int i = 0; i < matrix.Columns.Count; i++)
            {
                types[i] = new ComboBox();
                types[i].Name = "Combos" + i.ToString();
                types[i].Items.Add("String");
                types[i].Items.Add("Boolean");
                types[i].Items.Add("Integer");
                types[i].Items.Add("Float");

                types[i].Width = 100;

                if (i == 0) types[i].SelectedIndex = 0;
                else types[i].SelectedIndex = 3;
                DockPanel.SetDock(types[i], Dock.Top);
                Border container = new Border();

                changeNames[i] = new Button();
                changeNames[i].Width = 100;
                changeNames[i].Height = 25;
                changeNames[i].Content = "Change name";
                changeNames[i].Name = "Button" + i.ToString();
                changeNames[i].Click += new RoutedEventHandler(button_click);
                changeNames[i].MouseEnter += new MouseEventHandler(button_MouseEnter);
                buttonsPanel.Children.Add(changeNames[i]);

                load[i] = new CheckBox();
                Grid auxPanel = new Grid();
                load[i].IsChecked = true;
                load[i].HorizontalAlignment = HorizontalAlignment.Center;
                load[i].VerticalAlignment = VerticalAlignment.Center;
                auxPanel.Width = 100;
                auxPanel.Height = 25;
                //auxPanel.LastChildFill = true;
                auxPanel.Children.Add(load[i]);
                checkPanel.Children.Add(auxPanel);



                //container.Child = types[i];
                stackPanel1.Children.Add(types[i]);

                types[i].ContextMenu = context;

                types[i].MouseEnter += new MouseEventHandler(mouseEnter);

                //dockPanel1.Children.Add(types[i]);

                //row[i] = types[i];
            }

            //_datagrid.Rows.Add(row);

            for (int i = 0; i < matrix.Rows.Count; i++)
            {
                DataRow aux = _datagrid.NewRow();
                DataRow aux2 = copy.NewRow();
                for (int j = 0; j < matrix.Columns.Count; j++)
                {
                    aux[j] = matrix.Rows[i][j];
                    aux2[j] = matrix.Rows[i][j];
                }
                _datagrid.Rows.Add(aux);

                copy.Rows.Add(aux2);
            }


            InitializeComponent();

            dockPanel3.LastChildFill = false;

            dockPanel4.Children.Add(stackPanel1);

            buttons.Children.Add(buttonsPanel);
            checks.Children.Add(checkPanel);

            //datagrid.ItemsSource.
            /*DataGrid container = new DataGrid();
            container.DataSource = matrix.getTable();
            //Create a DataGridTableStyle object
            DataGridTableStyle dgdtblStyle = new DataGridTableStyle();
            //Set its properties
            dgdtblStyle.MappingName = dtblFunctionalArea.TableName;//its table name of dataset
            dgdFunctionArea.TableStyles.Add(dgdtblStyle);
            dgdtblStyle.RowHeadersVisible = false;
            dgdtblStyle.HeaderBackColor = Color.LightSteelBlue;
            dgdtblStyle.AllowSorting = false;
            dgdtblStyle.HeaderBackColor = Color.FromArgb(8, 36, 107);
            dgdtblStyle.RowHeadersVisible = false;
            dgdtblStyle.HeaderForeColor = Color.White;
            dgdtblStyle.HeaderFont = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            dgdtblStyle.GridLineColor = Color.DarkGray;
            dgdtblStyle.PreferredRowHeight = 22;
            dgdFunctionArea.BackgroundColor = Color.White; 
            this.Content = container;*/
        }




        public DataTable datagrid
        { get { return _datagrid; } }


        public string type(int i)
        {
            return types[i].SelectedItem.ToString();
        }

        private void table_MouseUp(object sender, MouseEventArgs e)
        {

            int i;
            bool found = false;
            for (i = 0; i < table.Columns.Count && !false; i++)
            {
                if (table.IsMouseCaptured)
                    found = true;
            }

            //context.Placement = table;
            context.IsOpen = true;
            //.Show(dataGridView, new Point(e.X, e.Y));

        }

        private void Change_name(object sender, RoutedEventArgs e)
        {
            int i;

            /*bool found = false;

            
            for (i = 0; i < table.Columns.Count && !found; i++)
                if (types[i].ContextMenu = (sender as ContextMenu))
                    found = true;
            i--;*/


            changeColName change = new changeColName(table.Columns[columnMouseOver].Header.ToString());

            change.ShowDialog();

            if (change.accept())
            {
                //MessageBox.Show(change.newName());
                table.Columns[columnMouseOver].Header = change.newName();
            }
        }

        void button_click(object sender, RoutedEventArgs e)
        {
            int i;

            /*bool found = false;

            
            for (i = 0; i < table.Columns.Count && !found; i++)
                if (types[i].ContextMenu = (sender as ContextMenu))
                    found = true;
            i--;*/


            changeColName change = new changeColName(table.Columns[columnMouseOver].Header.ToString());

            change.ShowDialog();

            if (change.accept())
            {
                //MessageBox.Show(change.newName());
                table.Columns[columnMouseOver].Header = change.newName();
            }
        }

        private void mouseEnter(object sender, MouseEventArgs e)
        {
            int i;

            bool found = false;


            for (i = 0; i < table.Columns.Count && !found; i++)
                if (types[i].Name == (sender as ComboBox).Name)
                    found = true;

            i--;
            columnMouseOver = i;
        }

        void button_MouseEnter(object sender, MouseEventArgs e)
        {
            int i;

            bool found = false;


            for (i = 0; i < table.Columns.Count && !found; i++)
                if (changeNames[i].Name == (sender as Button).Name)
                    found = true;

            i--;
            columnMouseOver = i;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            canceled = false;
            for (int i = 0; i < _datagrid.Columns.Count; i++)
                _datagrid.Columns[i].Caption = table.Columns[i].Header.ToString();
            
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            this.refresh();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.refresh();
            
        }

        private void refresh()
        {
            /*for (int i = 0; i < _datagrid.Columns.Count; i++)
            {
                table.Columns[i].Header = _datagrid.Columns[i].Caption;
                for (int j = 0; j < _datagrid.Rows.Count; j++)
                    table..ro = _datagrid.Columns[i][j];
            }*/

            while (_datagrid.Rows.Count > 0)
                _datagrid.Rows.Remove(_datagrid.Rows[0]);

            for (int i = 0; i < copy.Rows.Count; i++)
            {
                DataRow row = _datagrid.NewRow();
                for (int j = 0; j < copy.Columns.Count; j++)
                    row[j] = copy.Rows[i][j];

                _datagrid.Rows.Add(row);
            }

            //MessageBox.Show(_datagrid.Rows[0][1].ToString());

            /*DataSet ds = new DataSet();
            ds.Tables.Add(_datagrid);
            table.ItemsSource = ds.Tables[0].DefaultView;*/
        }

        public string getData(int i, int j)
        {
            return (_datagrid.Rows[i][j].ToString());
        }

        public string getColumnTitle(int i)
        {
            return _datagrid.Columns[i].Caption;
        }

        public int getNCols()
        {
            return _datagrid.Columns.Count;
        }

        public int getNRows()
        {
            return _datagrid.Rows.Count;
        }

        public bool loadColumn(int i)
        {
            if (Convert.ToBoolean(load[i].IsChecked)) return true;
            else return false;
        }

        public bool result()
        {
            return !canceled;
        }

        private void table_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
        {

        }

    }
}

